library(ggplot2)
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
library(plotly)
## 
## Attaching package: 'plotly'
## The following objects are masked from 'package:plyr':
## 
##     arrange, mutate, rename, summarise
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ads <- read.csv("Advertising.csv")

summary(ads)
##        X                TV             radio          newspaper     
##  Min.   :  1.00   Min.   :  0.70   Min.   : 0.000   Min.   :  0.30  
##  1st Qu.: 50.75   1st Qu.: 74.38   1st Qu.: 9.975   1st Qu.: 12.75  
##  Median :100.50   Median :149.75   Median :22.900   Median : 25.75  
##  Mean   :100.50   Mean   :147.04   Mean   :23.264   Mean   : 30.55  
##  3rd Qu.:150.25   3rd Qu.:218.82   3rd Qu.:36.525   3rd Qu.: 45.10  
##  Max.   :200.00   Max.   :296.40   Max.   :49.600   Max.   :114.00  
##      sales      
##  Min.   : 1.60  
##  1st Qu.:10.38  
##  Median :12.90  
##  Mean   :14.02  
##  3rd Qu.:17.40  
##  Max.   :27.00
mod <- lm(sales ~ TV, data = ads)
summary(mod)
## 
## Call:
## lm(formula = sales ~ TV, data = ads)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.3860 -1.9545 -0.1913  2.0671  7.2124 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 7.032594   0.457843   15.36   <2e-16 ***
## TV          0.047537   0.002691   17.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.259 on 198 degrees of freedom
## Multiple R-squared:  0.6119, Adjusted R-squared:  0.6099 
## F-statistic: 312.1 on 1 and 198 DF,  p-value: < 2.2e-16
g1 <- ggplot(ads, aes(TV, sales)) +
  geom_point()

g2 <- ggplot(ads, aes(radio, sales)) +
  geom_point()

g3 <- ggplot(ads, aes(newspaper, sales)) +
  geom_point()

multiplot(g1, g2, g3)

p <- plot_ly(data = ads, z = ~sales, x = ~TV, y = ~radio, opacity = 0.5) %>%
  add_markers(marker = list(size = 2))

x <- seq(0.70, 296.40, length = 100)
y <- seq(0, 49.6, length = 100)

mod2 <- lm(sales ~ TV + radio, data = ads)
summary(mod2)
## 
## Call:
## lm(formula = sales ~ TV + radio, data = ads)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.7977 -0.8752  0.2422  1.1708  2.8328 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.92110    0.29449   9.919   <2e-16 ***
## TV           0.04575    0.00139  32.909   <2e-16 ***
## radio        0.18799    0.00804  23.382   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.681 on 197 degrees of freedom
## Multiple R-squared:  0.8972, Adjusted R-squared:  0.8962 
## F-statistic: 859.6 on 2 and 197 DF,  p-value: < 2.2e-16
plane <- outer(x, y, function(a, b){summary(mod2)$coef[1,1] + summary(mod2)$coef[2,1]*a + summary(mod2)$coef[3,1]*b})

p %>%
  add_surface(x = ~x, y = ~y, z = ~plane, showscale = TRUE)
mod3 <- lm(sales ~ TV*radio, data = ads)

plane1 <- outer(x, y, function(a, b){summary(mod3)$coef[1,1] + summary(mod3)$coef[2,1]*a + summary(mod3)$coef[3,1]*b + summary(mod3)$coef[4,1]*a*b})

p %>%
  add_surface(x = ~x, y = ~y, z = ~plane1, showscale = TRUE)